<?php
/**
 * @title 313. 超级丑数 - 动态规划
 * @author start2004
 */

class Solution {

    /**
     * @param Integer $n
     * @param Integer[] $primes
     * @return Integer
     */
    function nthSuperUglyNumber($n, $primes) {
        /**
         * @since 2020-12-23 丑数数组
         */
        $nums = [1];

        /**
         * @since 2020-12-23 长度
         */
        $len = count($primes);

        /**
         * @since 2020-12-23 指针数组
         */
        $pArray = array_fill(0, $len, 0);
        
        /**
         * @since 2020-12-23 迭代
         */
        for($i=1; $i<$n; $i++){
            /**
             * @since 2020-12-23 最小丑数
             */
            $min = PHP_INT_MAX;
            $minKey = [];
            for($j=0; $j<$len; $j++){
                $m = $nums[$pArray[$j]]*$primes[$j];
                if($m < $min){
                    $min = $m;
                    $minKey = [$j];
                } elseif($m == $min) {
                    $minKey[] = $j;
                } else {}
            }
            $nums[] = $min;

            /**
             * @since 2020-12-23 移动指针
             */
            foreach($minKey as $j){
                $pArray[$j] += 1;
            }
        }

        /**
         * @return
         */
        return $nums[$n-1];
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.30_12.php
 */
$datas = [
    [12, [2,7,13,19]], // 32
    [1, [2,3,5]], // 1
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->nthSuperUglyNumber($data[0], $data[1]);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
